package com.msr.controller;

import com.msr.bean.Desk;
import com.msr.service.DeskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author tom
 * @version V1.0
 * @Package com.msr.controller
 * @date 2024/1/10 9:59
 * @Copyright © 株式会社多言语系统研究所
 */
@Controller
@RequestMapping("/desk")
public class DeskController {
    @Autowired
    private DeskService deskService;

    @RequestMapping("/deskList")
    public String deskList(String name, Model model){
        List<Desk> deskList = deskService.deskAll(name);
        model.addAttribute("deskList",deskList);
        return "deskList";
    }

    /**
     * 保存餐桌
     * @param name
     * @return
     */
    @RequestMapping("/saveDesk")
    public void saveDesk(String name, HttpServletResponse response) throws IOException {
        //处理乱码
        response.setContentType("text/html;charset=utf-8");
        //业务代码处理
        int num = deskService.saveDesk(name);
        if(num>0){
            response.getWriter().write("<script>alert('餐桌添加成功！');location.href='/desk/deskList';</script>");
        }else{
            response.getWriter().write("<script>alert('餐桌添加失败！');</script>");
        }
    }

    /**
     * 删除餐桌
     * @param response
     * @throws IOException
     */
    @RequestMapping("/delDesk/{id}")
    public void delDesk(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException {
        //处理乱码
        response.setContentType("text/html;charset=utf-8");
        //业务代码处理
        int num = deskService.delDesk(id);
        if(num>0){
            response.getWriter().write("<script>alert('餐桌删除成功！');location.href='/desk/deskList';</script>");
        }else{
            response.getWriter().write("<script>alert('餐桌删除失败！');</script>");
        }
    }

    @RequestMapping("/changeState/{id}/{state}")
    public void changeState(@PathVariable("id") Integer id,@PathVariable("state") Integer state, HttpServletResponse response) throws IOException {
        //处理乱码
        response.setContentType("text/html;charset=utf-8");

        Desk desk = new Desk();
        desk.setId(id);
        desk.setState(state);

        //判断是预定还是退桌
        if(state==0){ //退桌
            //清空时间
            desk.setBooktime(null);
        }else{  //预定
            Date sysDate = new Date();
            //String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sysDate);
            desk.setBooktime(sysDate);
        }
        //业务代码处理
        int num = deskService.changeState(desk);
        String msg = "";
        if(num>0){
            msg = "操作成功！";
            /*if(state==0){//退桌成功
                msg = "退桌成功";
            }else{//预定成功
                msg = "退桌";
            }*/
            response.getWriter().write("<script>alert('"+msg+"');location.href='/desk/deskList';</script>");

        }else{
            msg = "操作失败";
            response.getWriter().write("<script>alert('"+msg+"');</script>");
        }
    }


}
